//
//  THJAddressSelectVC.m
//  THJXHW
//
//  Created by 河南德艺 on 2017/12/15.
//  Copyright © 2017年 河南德艺. All rights reserved.
//

#import "THJAddressSelectVC.h"
#import "ZBNetworking.h"
@interface THJAddressSelectVC ()<UITableViewDelegate,UITableViewDataSource>
{
    NSMutableString * address;
}
@property(nonatomic,strong)NSArray * dataArr;
@end

@implementation THJAddressSelectVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = SingleColor(whiteColor);
   
    // Do any additional setup after loading the view.
    [self getData];
}

-(void)getData{
    address = [NSMutableString new];
    [MBProgressHUD showMessage:@"" toView:self.view];
    __weak typeof(self) weakself = self;
    [ZBRequestManager requestWithConfig:^(ZBURLRequest *request){
        request.urlString= self.urlStr;
        NSLog(@"sj-url--%@",request.urlString);
        request.apiType=ZBRequestTypeCache;
    }  success:^(id responseObject,apiType type){

        NSDictionary * receiveData = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
        [MBProgressHUD hideHUDForView:weakself.view animated:YES];
        
        self.dataArr = receiveData[@"data"];
        NSLog(@"datacount-%ld",self.dataArr.count);
        if(self.dataArr.count){
            [self setTableviewWithFrame:CGRectMake(0, 20, ScreenWidth, ScreenHeight - 20) RegisterNibNameArr:@[] ReuseIdentifierArr:@[] RowHeight:44 BgColor:SingleColor(whiteColor) IsLoadMore:NO];
        }else{
            [self.navigationController popViewControllerAnimated:YES];
        }
            
        
    } failed:^(NSError *error){
    }];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArr.count;
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }
    NSDictionary * dic = self.dataArr[indexPath.row];
    cell.textLabel.text = dic[@"areaname"];
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSDictionary * dic = self.dataArr[indexPath.row];
    self.urlStr =  [NSString stringWithFormat:@"%@index.php?f=Area&r=Area&parentid=%@",THJUrlDomain,dic[@"areaid"]];
    [self getData];
    [address appendFormat:@"%@",dic[@"areaname"]];
    [self.delegate selectedAddress:address AreaId:dic[@"areaid"]];
}
@end
